import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class ChangeServer extends JFrame {

	private JPanel contentPane;

	private static File f;
	private static FileOutputStream fos;
	
	
	private static final String s10005 = 
					"<?xml version='1.0' encoding='UTF-8'?>" + "\n" +
					"<ipconfig>" + "\n" +
					"	<ip>192.168.1.14</ip>" + "\n" +
					"	<port>7777</port>" + "\n" +
					"	<zone>10005</zone>" + "\n" +
					"</ipconfig>";
	
	private static final String s10007 = 
			"<?xml version='1.0' encoding='UTF-8'?>" + "\n" +
			"<ipconfig>" + "\n" +
			"	<ip>192.168.1.56</ip>" + "\n" +
			"	<port>7777</port>" + "\n" +
			"	<zone>10007</zone>" + "\n" +
			"</ipconfig>";


	JLabel label = new JLabel("");
	
	private void back() {
		setVisible(!isVisible());
	}


	public static void main(String[] args) throws Exception {
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChangeServer frame;
					frame = new ChangeServer();
					frame.setVisible(true);
					try{
						f = new File("Z:\\res\\data\\Xml\\ipconfig.cfg");
						fos = new FileOutputStream(f);
					} catch (java.io.FileNotFoundException e) {
						frame.label.setText("异常! 重启程序再试" + e.getMessage());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	
		
	}

	/**
	 * Create the frame.
	 */
	public ChangeServer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JButton btnNewButton = new JButton("切换到10007区");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					f.delete();
					fos.write(s10007.getBytes());
					label.setText("10007");
					back();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		contentPane.add(btnNewButton, BorderLayout.NORTH);

		JButton button = new JButton("切换到10005区");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					fos.write(s10005.getBytes());
					label.setText("10005");
					back();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		contentPane.add(button, BorderLayout.SOUTH);
		
		contentPane.add(label, BorderLayout.CENTER);
		
		addT();
	}

	private void addT() {
		if (SystemTray.isSupported()) {// 判断系统是否支持系统托盘功能
			URL resource = this.getClass().getResource("icon.png");
			ImageIcon icon = new ImageIcon(resource);
			
			PopupMenu popupMenu = new PopupMenu();
			MenuItem item1 = new MenuItem("退出");
			MenuItem item2 = new MenuItem("隐藏/显示");
			MenuItem item3 = new MenuItem("设置");
			item1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			item2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					back();
				}
			});
			item3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					///////////////////////////////////
				}
			});
			popupMenu.add(item3);
			popupMenu.add(item2);
			popupMenu.add(item1);
			
			TrayIcon trayIcon = new TrayIcon(icon.getImage(), "双击 显示/隐藏 主面板,右键弹出退出菜单",popupMenu);
			trayIcon.addMouseListener(new MouseAdapter(){
				public void mousePressed (MouseEvent e){
					if(e.getClickCount()==2){
						back();
					}
				}
			});
			
			
			SystemTray systemTray = SystemTray.getSystemTray();
			try {
				systemTray.add(trayIcon);
			} catch (AWTException e1) {
			}
		}
	}

}
